package com.buddies.lottery.utils;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import com.buddies.lottery.App;
import com.buddies.lottery.R;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class LotteryUtils {

    public static final DecimalFormat FLOAT_FORMAT = new DecimalFormat("#.####");
    public static final DecimalFormat BALANCE_FORMAT = new DecimalFormat("0.00");
    public static final DecimalFormat PERIOD_NUM_FORMAT = new DecimalFormat("000");

    public static final int LOTTERY_BJPK10 = 4;
    public static final int LOTTERY_JSSC = 5;
    public static final int LOTTERY_XYFT = 6;
    public static final int LOTTERY_JSFT = 7;
    public static final int LOTTERY_XG6HC = 9;

    public static final Map<String, Integer> XG6HC_COLOR_NUM_MAP = new HashMap<String, Integer>() {
        {
            put("3", R.drawable.dot_blue_14);
            put("4", R.drawable.dot_blue_14);
            put("9", R.drawable.dot_blue_14);
            put("10", R.drawable.dot_blue_14);
            put("14", R.drawable.dot_blue_14);
            put("15", R.drawable.dot_blue_14);
            put("20", R.drawable.dot_blue_14);
            put("25", R.drawable.dot_blue_14);
            put("26", R.drawable.dot_blue_14);
            put("31", R.drawable.dot_blue_14);
            put("36", R.drawable.dot_blue_14);
            put("37", R.drawable.dot_blue_14);
            put("41", R.drawable.dot_blue_14);
            put("42", R.drawable.dot_blue_14);
            put("47", R.drawable.dot_blue_14);
            put("48", R.drawable.dot_blue_14);
            put("5", R.drawable.dot_green);
            put("6", R.drawable.dot_green);
            put("11", R.drawable.dot_green);
            put("16", R.drawable.dot_green);
            put("17", R.drawable.dot_green);
            put("21", R.drawable.dot_green);
            put("22", R.drawable.dot_green);
            put("27", R.drawable.dot_green);
            put("28", R.drawable.dot_green);
            put("32", R.drawable.dot_green);
            put("33", R.drawable.dot_green);
            put("38", R.drawable.dot_green);
            put("39", R.drawable.dot_green);
            put("43", R.drawable.dot_green);
            put("44", R.drawable.dot_green);
            put("49", R.drawable.dot_green);
            put("1", R.drawable.dot_red);
            put("2", R.drawable.dot_red);
            put("7", R.drawable.dot_red);
            put("8", R.drawable.dot_red);
            put("12", R.drawable.dot_red);
            put("13", R.drawable.dot_red);
            put("18", R.drawable.dot_red);
            put("19", R.drawable.dot_red);
            put("23", R.drawable.dot_red);
            put("24", R.drawable.dot_red);
            put("29", R.drawable.dot_red);
            put("30", R.drawable.dot_red);
            put("34", R.drawable.dot_red);
            put("35", R.drawable.dot_red);
            put("40", R.drawable.dot_red);
            put("45", R.drawable.dot_red);
            put("46", R.drawable.dot_red);
        }
    };

    public static final Map<String, Integer> PK10_COLOR_NUM_MAP = new HashMap<String, Integer>() {
        {
            put("1", R.drawable.dot_yellow);
            put("2", R.drawable.dot_deep_blue);
            put("3", R.drawable.dot_deep_gray);
            put("4", R.drawable.dot_orange);
            put("5", R.drawable.dot_blue_14);
            put("6", R.drawable.dot_purple);
            put("7", R.drawable.dot_gray);
            put("8", R.drawable.dot_red);
            put("9", R.drawable.dot_brown);
            put("10", R.drawable.dot_green);
        }
    };

    public static Map<String, Integer> getLotteryColorNumMap(int lotteryId) {
        Map<String, Integer> colorNumMap = null;
        if (lotteryId == LotteryUtils.LOTTERY_XG6HC) {
            colorNumMap = LotteryUtils.XG6HC_COLOR_NUM_MAP;
        } else if (lotteryId == LotteryUtils.LOTTERY_BJPK10 ||
                lotteryId == LotteryUtils.LOTTERY_JSSC ||
                lotteryId == LotteryUtils.LOTTERY_XYFT ||
                lotteryId == LotteryUtils.LOTTERY_JSFT) {
            colorNumMap = LotteryUtils.PK10_COLOR_NUM_MAP;
        }
        return colorNumMap;
    }

    public static String formatLotteryPeriod(String period) {
        if (TextUtils.isEmpty(period)) {
            period = "...";
        }
        return String.format("第%s期", period);
    }

    public static SpannableString formatLotteryNextPeriod(String period) {
        if (TextUtils.isEmpty(period)) {
            period = "休息中";
        } else {
            period = String.format("第%s期", period);
        }

        SpannableString spannableString = new SpannableString(period);
        spannableString.setSpan(new ForegroundColorSpan(App.getInstance().getResources().getColor(R.color.red)), period.length() > 3 ? 1 : 0, period.length() > 3 ? period.length() - 1 : period.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public static String[] parseWinNum(String winNum) {
        String[] result = null;
        if (!TextUtils.isEmpty(winNum)) {
            result = winNum.split(" ");
        }
        return result;
    }

    public static String getLotteryTabQueryId(int lotteryId, int tabId) {
        return String.format("%s_%s", lotteryId, tabId);
    }

    public static String formatMoney(double money) {
        return FLOAT_FORMAT.format(money);
    }

    public static String formatBalance(double money) {
        return BALANCE_FORMAT.format(money);
    }

    public static int getXG6HCNumColor(String num) {
        return getNumColor(num, XG6HC_COLOR_NUM_MAP);
    }

    public static int getNumColor(String num, Map<String, Integer> colorNumsMap) {
        if (TextUtils.isEmpty(num) || !TextUtils.isDigitsOnly(num)) {
            return 0;
        }

        String key = num;
        if (key.startsWith("0")) {
            key = key.substring(1);
        }

        Integer color = colorNumsMap.get(key);
        if (color != null) {
            return color;
        }

        return 0;
    }

}
